package com.ruoyi.production.service.impl.regulator;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.regulator.RegulatorAmount;
import com.ruoyi.production.mapper.regulator.RegulatorAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.regulator.RegulatorPurchaseMapper;
import com.ruoyi.production.domain.regulator.RegulatorPurchase;
import com.ruoyi.production.service.regulator.IRegulatorPurchaseService;

/**
 * 调节剂购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class RegulatorPurchaseServiceImpl implements IRegulatorPurchaseService
{
    @Autowired
    private RegulatorPurchaseMapper regulatorPurchaseMapper;
    @Autowired
    private RegulatorAmountMapper regulatorAmountMapper;

    /**
     * 查询调节剂购买信息
     *
     * @param regulatorPurchaseId 调节剂购买信息ID
     * @return 调节剂购买信息
     */
    @Override
    public RegulatorPurchase selectRegulatorPurchaseById(Long regulatorPurchaseId)
    {
        return regulatorPurchaseMapper.selectRegulatorPurchaseById(regulatorPurchaseId);
    }

    /**
     * 查询调节剂购买信息列表
     *
     * @param regulatorPurchase 调节剂购买信息
     * @return 调节剂购买信息
     */
    @Override
    public List<RegulatorPurchase> selectRegulatorPurchaseList(RegulatorPurchase regulatorPurchase)
    {
        return regulatorPurchaseMapper.selectRegulatorPurchaseList(regulatorPurchase);
    }

    /**
     * 新增调节剂购买信息
     *
     * @param regulatorPurchase 调节剂购买信息
     * @return 结果
     */
    @Override
    public int insertRegulatorPurchase(RegulatorPurchase regulatorPurchase)
    {
        regulatorPurchase.setCreateTime(DateUtils.getNowDate());
        return regulatorPurchaseMapper.insertRegulatorPurchase(regulatorPurchase);
    }

    /**
     * 修改调节剂购买信息
     *
     * @param regulatorPurchase 调节剂购买信息
     * @return 结果
     */
    @Override
    public int updateRegulatorPurchase(RegulatorPurchase regulatorPurchase)
    {
        Long regulatorPurchaseId = regulatorPurchase.getRegulatorPurchaseId();
        //数据库存储的数据
        RegulatorPurchase PurchaseDB = selectRegulatorPurchaseById(regulatorPurchaseId);
        //数据库存储的原数据的总量
        RegulatorAmount originAmounts = new RegulatorAmount();
        originAmounts.setRegulatorTypeId(PurchaseDB.getRegulatorTypeId());
        originAmounts.setRegulatorNameId(PurchaseDB.getRegulatorNameId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setContent(PurchaseDB.getContent());
        List<RegulatorAmount> amounts = regulatorAmountMapper.selectRegulatorAmountList(originAmounts);
        RegulatorAmount originAmount = amounts.get(0);
        if (PurchaseDB.getRegulatorTypeId().equals(regulatorPurchase.getRegulatorTypeId()) &&
                PurchaseDB.getRegulatorNameId().equals(regulatorPurchase.getRegulatorNameId()) &&
                PurchaseDB.getContent().equals(regulatorPurchase.getContent()) &&
                PurchaseDB.getOriginCompany().equals(regulatorPurchase.getOriginCompany())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+regulatorPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            regulatorAmountMapper.updateRegulatorAmount(originAmount);
        }else {
            RegulatorAmount newAmounts = new RegulatorAmount();
            newAmounts.setRegulatorTypeId(regulatorPurchase.getRegulatorTypeId());
            newAmounts.setRegulatorNameId(regulatorPurchase.getRegulatorNameId());
            newAmounts.setOriginCompany(regulatorPurchase.getOriginCompany());
            newAmounts.setContent(regulatorPurchase.getContent());
            List<RegulatorAmount> amounts1 = regulatorAmountMapper.selectRegulatorAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(regulatorPurchase.getPurchaseAmount());
                regulatorAmountMapper.insertRegulatorAmount(newAmounts);
            }else {
                RegulatorAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+regulatorPurchase.getPurchaseAmount());
                regulatorAmountMapper.updateRegulatorAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            regulatorAmountMapper.updateRegulatorAmount(originAmount);
        }
        return regulatorPurchaseMapper.updateRegulatorPurchase(regulatorPurchase);
    }

    /**
     * 批量删除调节剂购买信息
     *
     * @param regulatorPurchaseIds 需要删除的调节剂购买信息ID
     * @return 结果
     */
    @Override
    public int deleteRegulatorPurchaseByIds(Long[] regulatorPurchaseIds)
    {
        for (Long regulatorPurchaseId : regulatorPurchaseIds) {
            RegulatorPurchase regulatorPurchase = selectRegulatorPurchaseById(regulatorPurchaseId);
            RegulatorAmount regulatorAmount = new RegulatorAmount();
            regulatorAmount.setRegulatorTypeId(regulatorPurchase.getRegulatorTypeId());
            regulatorAmount.setRegulatorNameId(regulatorPurchase.getRegulatorNameId());
            regulatorAmount.setOriginCompany(regulatorPurchase.getOriginCompany());
            regulatorAmount.setContent(regulatorPurchase.getContent());
            List<RegulatorAmount> amounts = regulatorAmountMapper.selectRegulatorAmountList(regulatorAmount);
            RegulatorAmount regulatorAmount1 = amounts.get(0);
            regulatorAmount1.setStorageAmount(regulatorAmount1.getStorageAmount() - regulatorPurchase.getPurchaseAmount());
            regulatorAmountMapper.updateRegulatorAmount(regulatorAmount1);
        }
        return regulatorPurchaseMapper.deleteRegulatorPurchaseByIds(regulatorPurchaseIds);
    }

    /**
     * 删除调节剂购买信息信息
     *
     * @param regulatorPurchaseId 调节剂购买信息ID
     * @return 结果
     */
    @Override
    public int deleteRegulatorPurchaseById(Long regulatorPurchaseId)
    {
        RegulatorPurchase regulatorPurchase = selectRegulatorPurchaseById(regulatorPurchaseId);
        RegulatorAmount regulatorAmount = new RegulatorAmount();
        regulatorAmount.setRegulatorTypeId(regulatorPurchase.getRegulatorTypeId());
        regulatorAmount.setRegulatorNameId(regulatorPurchase.getRegulatorNameId());
        regulatorAmount.setOriginCompany(regulatorPurchase.getOriginCompany());
        regulatorAmount.setContent(regulatorPurchase.getContent());
        List<RegulatorAmount> amounts = regulatorAmountMapper.selectRegulatorAmountList(regulatorAmount);
        RegulatorAmount regulatorAmount1 = amounts.get(0);
        regulatorAmount1.setStorageAmount(regulatorAmount1.getStorageAmount() - regulatorPurchase.getPurchaseAmount());
        regulatorAmountMapper.updateRegulatorAmount(regulatorAmount1);
        return regulatorPurchaseMapper.deleteRegulatorPurchaseById(regulatorPurchaseId);
    }

    /**
     * 插入调节剂购买表更新调节剂量表
     * @param regulatorPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(RegulatorPurchase regulatorPurchase){
        RegulatorAmount regulatorAmount = new RegulatorAmount();
        regulatorAmount.setRegulatorTypeId(regulatorPurchase.getRegulatorTypeId());
        regulatorAmount.setRegulatorNameId(regulatorPurchase.getRegulatorNameId());
        regulatorAmount.setContent(regulatorPurchase.getContent());
        regulatorAmount.setOriginCompany(regulatorPurchase.getOriginCompany());
        List<RegulatorAmount> regulatorAmounts = regulatorAmountMapper.selectRegulatorAmountList(regulatorAmount);
        regulatorAmount.setStorageAmount(regulatorPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(regulatorAmounts)) {
            RegulatorAmount amount = regulatorAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + regulatorPurchase.getPurchaseAmount());
            regulatorAmountMapper.updateRegulatorAmount(amount);
        } else {
            regulatorAmountMapper.insertRegulatorAmount(regulatorAmount);
        }
        return insertRegulatorPurchase(regulatorPurchase);
    }
}
